<template>
  <div class="department" id="department">
    <!-- 左侧部门管理 -->

    <!-- 右侧 -->
    <div class="department_right">
      <div class="mechanism_nav app-container-search">
        <el-form ref="form" :model="getList" label-width="110px">
          <el-row :gutter="20">
            <el-col :span="6"
              ><el-form-item label="项目名称：">
                <el-select
                  class="selectBhz"
                  v-model="getList.fprojectid"
                  placeholder="所有项目"
                  @change="selectProject"
                >
                  <el-option
                    v-for="v in projectsObj"
                    :key="v.fItemName"
                    :label="v.fItemName"
                    :value="v.fItemValue"
                  >
                  </el-option>
                </el-select> </el-form-item
            ></el-col>
            <el-col :span="6"
              ><el-form-item label="标段名称：">
                <el-select
                  class="selectBhz"
                  v-model="getList.fCompanyid"
                  placeholder="所有标段"
                  @change="selectTender"
                >
                  <el-option
                    v-for="v in tendersObj"
                    :key="v.fItemName"
                    :label="v.fItemName"
                    :value="v.fItemValue"
                  >
                  </el-option>
                </el-select> </el-form-item
            ></el-col>
            <el-col :span="6"
              ><el-form-item label="拌合站名称：">
                <el-select
                  class="selectBhz"
                  v-model="getList.stationId"
                  placeholder="拌合站查询"
                  @change="selectStation"
                >
                  <el-option
                    v-for="v in bhzDataList"
                    :key="v.fItemName"
                    :label="v.fItemName"
                    :value="v.fItemValue"
                  >
                  </el-option>
                </el-select> </el-form-item
            ></el-col>
            <el-col :span="6">
              <el-form-item label="拌合机名称：">
                <el-select
                  class="selectBhz"
                  v-model="getList.machineId"
                  placeholder="拌合机查询"
                >
                  <el-option
                    v-for="v in bhjDataList"
                    :key="v.fItemName"
                    :label="v.fItemName"
                    :value="v.fItemValue"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>

          <div class="searchBtn">
            <el-button
              type="primary"
              :loading="editLoading"
              @click="yj_searchForm(getList)"
              >查询</el-button
            >
            <el-button @click="reset">重置</el-button>
          </div>
        </el-form>
      </div>
      <div class="app-container-btn">
        <el-button type="primary" @click="edit(0)">新增</el-button>
        <el-button type="primary" @click="edit(1)">编辑</el-button>
        <el-button type="primary" @click="SubmitText(fId)">提交</el-button>
        <el-button type="primary" @click="rowDelete(fId)">删除</el-button>
      </div>
      <!-- 表格 -->
      <div class="text_table TensionRealTime">
        <tableData
          @ClickOnLine="yj_viewForm"
          :page="getList.page"
          :pageSize="getList.pageSize"
          :stripe="false"
          height="100%"
          :config="table_config"
          :dataList="tableData"
          :loading="loading"
          @selection-change="selectionChange"
          @sort-change="sortChange"
          @click-change="rowChange"
        />

        <pagination
          :info="queryCriteriaData"
          :total="queryCriteriaData.total"
          :pageNum.sync="getList.page"
          :limit.sync="getList.pageSize"
          @pagination="yj_searchForm(getList)"
        />
      </div>
    </div>
    <messageBox
      :showMsgBox="showMsgBox"
      :msgTip="msgTip"
      :msgType="msgType"
      @confirmMsg="confirmMsg"
      @confirmMsgs="confirmMsgs"
    ></messageBox>
  </div>
</template>

<script>
import actions from "@/actions";
import messageBox from "@/components/public/messageBox"; //弹框
import TableData from "@/components/waterManage/public/el-tables"; //主页面列表
import Pagination from "@/components/public/el-pagination"; //主页面分页
import formJs from "./regexDetail.js"; //新增弹框配置项
import {
  getConcreteRecipeLists, //主页面列表接口
  getStationmachineList, //拌合站列表
  getMachineListByStationId, //拌合机
  deleteConcreteRecipe, //删除
  commitConcreteRecipe, //提交
} from "@/api/request.js";

export default {
  name: "HntMixProportion",
  data() {
    return {
      name: "HntMixProportion", //按钮配置页面代号
      showMsgBox: false,
      msgTip: "请先选中数据行！",
      msgType: 1,
      titelName: null, //弹框功能名称
      projectName: [], //下拉框项目名称
      bhzDataList: [], //拌合站
      bhjDataList: [], //拌合机
      projectsObj: [], //项目名
      editLoading: false,
      tendersObj: [],
      // 分页
      navigatepageNums: null, //是否显示分页
      queryCriteriaData: {},
      // 分页

      // 列表
      getList: {
        endDates: "",
        fCheckStatus: "",
        fCloseoperation: "",
        fCompanyid: "",
        fprojectid: "",
        machineId: "",
        modelType: "",
        page: 1,
        pageSize: 15,
        startDates: "",
        stationId: "",
        strengthgrade: "",
        userId: "",
        warningLevel: "",
      },

      loading: false, //列表转载
      fId: null, //当前行数据fId
      table_config: formJs.table_config,
      tableData: [], //表格列表数据列表
      row: {
        fStatus: "",
      }, //当前行
      // 列表
      userInfo: {}, //登录用户
    };
  },
  components: {
    TableData, //主页面列表
    Pagination, //分页
    messageBox,
  },
  mounted() {
    actions.onGlobalStateChange((state, prev) => {
      this.userInfo = prev.currentUser;
      // console.log("userInfo", this.userInfo);
      var data = prev.currentUser.projects;
      this.projectsObj = [
        {
          fItemName: "所有项目",
          fItemValue: "",
        },
      ];
      data.map((item) => {
        this.projectsObj.push({
          fItemName: item.projectName,
          fItemValue: item.projectId,
        });
      });
    }, true);

   
    this.getDate();

    this.table_config.operation.cols = [
      {
        prop: "fStatus",
        type: "primary",
        handleRow: this.handleRow,
      },
    ];
    this.table_config.operation["specialClass"] = function (rowData) {
      var color = null;
      if (rowData == "未提交") {
        color = "red !important";
      } else if (rowData == "生产中") {
        color = "#409eff !important";
      } else if (rowData == "运输中") {
        color = "#e6a23c  !important";
      } else {
        color = "green !important";
      }
      return color;
    };
  },
  methods: {
    // 重置
    reset() {
      this.getList = {
        endDates: "",
        fCheckStatus: "",
        fCloseoperation: "",
        fCompanyid: "",
        fprojectid: "",
        machineId: "",
        modelType: "",
        page: 1,
        pageSize: 15,
        startDates: "",
        stationId: "",
        strengthgrade: "",
        userId: "",
        warningLevel: "",
      };
      // 列表页查询不传 this.userInfo.id
    },
    // 所选项目下拉框选择
    selectProject(e) {
      this.tendersObj = [];
      this.getList.fCompanyid = "";
      this.getList.stationId = "";
      this.getList.machineId = "";
      this.bhzDataList = [];
      this.bhjDataList = [];
      this.getTender(e,1);
    },
    // 调标段list
    async getTender(e,num) {
      await this.$http(
        " http://172.18.5.31:8300/IOTManagement/Base/Company/getGXJLBaseCompanyList.do?projectId=" +
          e,
        {
          headers: {
            "Content-Type": "application/json",
            Authorization: localStorage.getItem("access_token"),
          },
        }
      ).then((res) => {
        if (res.status == 200) {
          var data = res.data.data.GXCompanyList;
          this.tendersObj = [];
          if (data.length == 1) {
            data.map((item) => {
              this.tendersObj.push({
                fItemName: item.sectionName,
                fItemValue: item.sectionId,
              });
            });
            this.getList.fCompanyid = data[0].sectionId;
          } else {
            this.tendersObj = [
              {
                fItemName: "所有标段",
                fItemValue: "",
              },
            ];
            data.map((item) => {
              this.tendersObj.push({
                fItemName: item.sectionName,
                fItemValue: item.sectionId,
              });
            });
          }


          if (num==0) {
            // num==0 调列表；num==1 不调
            this.yj_searchForm(this.getList);
          }
        }
      });
    },
    //所有标段下来选择
    selectTender(e) {
      this.getList.stationId = "";
      this.getList.machineId = "";
      this.bhzDataList = [];
      this.bhjDataList = [];
      this.mixingPlantList(0, this.getList);
    },
    //拌合站下拉选择
    selectStation(e) {
      this.getList.machineId = "";
      this.bhjDataList = [];
      this.getbhjDataMethods(e, 4);
    },
    // 获取列表数据
    yj_searchForm(data) {
      this.loading = true;
      // var newData = {};
      window.sessionStorage.setItem("GxEmbedded_getList", JSON.stringify(data));
      var id;
      if (data.fCompanyid != "") {
        id = data.fCompanyid;
      } else {
        id = data.fprojectid;
      }
      var getList = {
        fCheckStatus: "0", //0- APP端查询  1-填报人列表  2-场站列表  3-现场列表
        modelType: "2", // 0代办 1已办 不传或传空 表示列表页查询
        page: data.page, //页码
        pageSize: data.pageSize, //每页条数
        fprojectid: data.fprojectid, // 查询条件项目ID
        stationId: data.stationId, // 查询条件拌合站ID
        machineId: data.machineId, // 查询条件拌合机ID
        fCompanyid: id, // 左侧选中组织机构id
        userId: this.userInfo.id, // 查询待办或已办时传 列表页查询不传 this.userInfo.id
      };
      getConcreteRecipeLists(getList)
        .then((res) => {
          if (res.errcode == 0) {
            var that = this;
            that.loading = false;
            that.tableData = res.data.pageInfo.list;
            that.queryCriteriaData = res.data.pageInfo;
            that.navigatepageNums =
              that.queryCriteriaData.navigatepageNums.length;
          } else {
            this.$message.error("列表", res.msg);
            this.loading = false;
          }
        })
        .catch(() => {
          console.log("列表");
          this.loading = false;
        });
    },

    // 获取拌合站数据
    mixingPlantList(num, datas) {
      var data = {
        companyId: datas.fCompanyid, // 施工标段id
        modelType: num, // 类型
        stationType: "1", // 拌合站类型
      };
      getStationmachineList(data)
        .then((res) => {
          if (res.errcode == 0) {
            var arr = res.data.stationmachineList;
            this.bhzDataList = [
              {
                fItemName: "所有拌和站",
                fItemValue: "",
              },
            ];
            arr.map((item) => {
              this.bhzDataList.push({
                fItemName: item.fMachinename,
                fItemValue: item.fMachineid,
              });
            });
          } else {
            this.$message.error(res.msg);
            return false;
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
    // 获取拌合机数据
    getbhjDataMethods(data) {
      getMachineListByStationId({ stationId: data })
        .then((res) => {
          if (res.errcode == 0) {
            var arr = res.data.stationmachineList;

            this.bhjDataList = [
              {
                fItemName: "所有拌和机",
                fItemValue: "",
              },
            ];
            arr.map((item) => {
              this.bhjDataList.push({
                fItemName: item.fMachinename,
                fItemValue: item.fMachineid,
              });
            });
          } else {
            this.$message.error(res.msg);
            return false;
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
    getDate() {
      if (JSON.parse(sessionStorage.getItem("GxEmbedded_getList"))) {
        this.getList = JSON.parse(sessionStorage.getItem("GxEmbedded_getList"));
        if (this.getList.fprojectid != "") {
          this.getTender(this.getList.fprojectid,0);
        } else {
          this.getList.fprojectid = this.projectsObj[1].fItemValue;
          this.getTender(this.getList.fprojectid,0);
        }

        if (this.getList.fCompanyid != "" && this.getList.fCompanyid != null) {
          this.mixingPlantList(0, this.getList);
        }

        if (this.getList.stationId != "" && this.getList.stationId != null) {
          this.getbhjDataMethods(this.getList.stationId);
        }

        if (this.getList.startDates == "" || this.getList.startDates == null) {
          this.getList.startDates = this.common.getDay(-90) + " " + "00:00:00"; // 开始时间
          this.getList.endDates = this.common.getDay(0) + " " + "23:59:59"; // 截至时间
        }
        this.timeDuring = [this.getList.startDates, this.getList.endDates];
      } else {
        this.getList.startDates = this.common.getDay(-90) + " " + "00:00:00"; // 开始时间
        this.getList.endDates = this.common.getDay(0) + " " + "23:59:59"; // 截至时间
        this.getList.fprojectid = this.projectsObj[1].fItemValue;
        this.getTender(this.getList.fprojectid,0);
        this.timeDuring = [
          this.common.getDay(-90) + " " + "00:00:00",
          this.common.getDay(0) + " " + "23:59:59",
        ];
      }

      
    },
    // 点击当前
    rowChange(val) {
      this.row = val;
      this.fId = val.fId;
    },
    // 点击查看
    yj_viewForm(row, label) {
      this.row = row;
      this.fId = row.fId;
      if (label == "配合比通知单编号") {
        this.$router.push({
          path: "/hntManage/mixNnotice",
          query: {
            data: JSON.stringify(row),
            num: 0, //0：直接点击配合比通知单编号 1：点击编辑按钮
          },
        });
      } else if (label == "生产任务单编号") {
        this.$router.push({
          path: "/hntManage/productionTasks",
          query: {
            data: JSON.stringify(row),
          },
        });
      }
    },

    // 不带功能的确定、取消按钮
    confirmMsg() {
      this.showMsgBox = false;
    },
    // 带功能确定
    confirmMsgs() {
      if (this.titelName == "提交") {
        this.loading = true;
        commitConcreteRecipe({
          fId: this.fId,
          userId: this.userInfo.id,
          userName: this.userInfo.userName,
        })
          .then((res) => {
            if (res.errcode == 0) {
              this.loading = false;
              this.showMsgBox = false;
              this.yj_searchForm(this.getList);
              this.$message.success("提交成功");
            } else {
              this.$message.error("提交失败", res.msg);
              this.loading = false;
            }
          })
          .catch(() => {
            this.loading = false;
          });
      } else if (this.titelName == "删除") {
        this.loading = true;
        deleteConcreteRecipe({ fId: this.fId })
          .then((res) => {
            if (res.errcode == 0) {
              this.loading = false;
              this.showMsgBox = false;
              this.yj_searchForm(this.getList);
              this.$message.success("删除成功");
            } else {
              this.$message.error("删除失败", res.msg);
              this.loading = false;
            }
          })
          .catch(() => {
            this.loading = false;
          });
      }
    },
    // 新增/编辑
    edit(num) {
      if (num == 0) {
        window.sessionStorage.removeItem("GxEmbedded_coordinate_treePeople");

        //新增
        if (this.getList.fCompanyid != "") {
          var CompanyName = null;
          var ProjectName = null;
          this.projectsObj.map((item) => {
            // 项目
            if (this.getList.fprojectid == item.fItemValue) {
              CompanyName = item.fItemName;
            }
          });
          this.tendersObj.map((item) => {
            // 标段
            if (this.getList.fCompanyid == item.fItemValue) {
              ProjectName = item.fItemName;
            }
          });
          this.$router.push({
            path: "/hntManage/hntMixProportionDetail",
            query: {
              status: 2,
            },
          });
          var id;
          if (this.getList.fCompanyid != "") {
            id = this.getList.fCompanyid;
          } else {
            id = this.getList.fprojectid;
          }
          var query = {
            fCompanyid: id,
            fCompanyName: CompanyName,
            num: num,
            fprojectid: id,
            fprojectName: ProjectName,
            fId: "",
          };
          window.sessionStorage.setItem(
            "GxEmbedded_HntMixProportion",
            JSON.stringify(query)
          );
        } else {
          this.msgTip = "请选择标段！";
          this.msgType = 1; // 0 带功能确定  1 无功能确定  2 带功能确定、取消
          this.showMsgBox = true;
        }
      } else if (num == 1) {
        // 编辑
        if (this.fId != null && this.fId != "") {
          if (this.row.fStatus == "未提交") {
            this.$router.push({
              path: "/hntManage/hntMixProportionDetail",
            });
            var query = {
              fCompanyid: this.row.fCompanyid,
              fCompanyName: this.row.fCompanyname,
              num: num,
              fprojectid: this.row.fprojectid,
              fprojectName: this.row.fProjectname,
              fId: this.fId,
            };
            window.sessionStorage.setItem(
              "GxEmbedded_HntMixProportion",
              JSON.stringify(query)
            );
          } else {
            this.msgTip = '请选择生产状态为"未提交"的数据！';
            this.msgType = 1; // 0 带功能确定  1 无功能确定  2 带功能确定、取消
            this.showMsgBox = true;
          }
        } else {
          this.msgTip = "请选择要编辑的数据！";
          this.msgType = 1; // 0 带功能确定  1 无功能确定  2 带功能确定、取消
          this.showMsgBox = true;
        }
      }
    },
    // 提交
    SubmitText(id) {
      if (id != "" && id != null) {
        if (this.row.fStatus == "未提交") {
          this.msgTip = "是否继续提交?";
          this.msgType = 2; // 0 带功能确定  1 无功能确定  2 带功能确定、取消
          this.showMsgBox = true;
          this.titelName = "提交";
        } else {
          this.msgTip = '请选择生产状态为"未提交"的数据！';
          this.msgType = 1; // 0 带功能确定  1 无功能确定  2 带功能确定、取消
          this.showMsgBox = true;
        }
      } else {
        this.msgTip = "请先选中要提交的数据！";
        this.msgType = 1; // 0 带功能确定  1 无功能确定  2 带功能确定、取消
        this.showMsgBox = true;
      }
    },

    // 删除
    rowDelete(id) {
      if (id != "" && id != null) {
        if (this.row.fStatus == "未提交") {
          this.msgTip = "此操作将永久删除该数据, 是否继续?";
          this.msgType = 2; // 0 带功能确定  1 无功能确定  2 带功能确定、取消
          this.showMsgBox = true;
          this.titelName = "删除";
        } else {
          this.msgTip = '请选择状态为"未提交"的数据！';
          this.msgType = 1; // 0 带功能确定  1 无功能确定  2 带功能确定、取消
          this.showMsgBox = true;
        }
      } else {
        this.msgTip = "请先选中要删除的数据！";
        this.msgType = 1; // 0 带功能确定  1 无功能确定  2 带功能确定、取消
        this.showMsgBox = true;
      }
    },

    selectionChange() {},
    // 排序方法
    sortChange() {},
  },
};
</script>

<style scoped>
.printTables {
  background: #fff;
  display: flex;
  justify-content: end;
  padding: 5px;
  margin-top: 10px;
}

.dialogBody {
  padding: 3px 20px;
  height: 80%;
  overflow-y: auto;
}

.baseTitle {
  /* border: 1px solid #0c204d; */
  border-bottom: 1px solid #0c204d;
  height: 30px;
  line-height: 30px;
  position: relative;
  padding-left: 15px;
}

.baseTitle::after {
  display: block;
  content: "";
  height: 30px;
  width: 5px;
  position: absolute;
  left: 0;
  top: 0;
  background: #0c204d;
}

/* 砼配合比表格 */
.mixProportion {
  width: 99%;
  height: 150px;
  margin: 20px 0;
  border-top: 1px solid #ccc;
  border-left: 1px solid #ccc;
  border-right: 1px solid #ccc;
}

.titleBig {
  width: 100%;
  height: 60px;
  border-bottom: 1px solid #ccc;
  display: flex;
  justify-content: space-between;
}

.title {
  height: 100%;
  width: 8%;
  display: flex;
  align-items: center;
  justify-content: center;
  border-left: 1px solid #ccc;
  font-size: 12px;
  font-weight: 700;
}

.title1 {
  display: flex;
  flex-direction: column;
}

.title1_1 {
  width: 100%;
  height: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
  border-bottom: 1px solid #ccc;
}

.title1_2 {
  width: 100%;
  height: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
}

.title1_2_1 {
  width: 33%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  border-left: 1px solid #ccc;
}

.title1_2_2 {
  width: 50%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  border-left: 1px solid #ccc;
}

.titleSmall {
  width: 100%;
  height: 30px;
  border-bottom: 1px solid #ccc;
  /* background: #0c204d; */
  display: flex;
}

.title2 {
  width: 8%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  border-left: 1px solid #ccc;
}

.titleSmall /deep/ .el-input__inner {
  height: 27px !important;
  line-height: 26px !important;
  text-align: center;
  border: none;
}

.titleSmall /deep/ .el-input {
  line-height: 28px !important;
}

/* 砼配合比表格 */
/* 任务单弹框 */
.Multi_level_header /deep/.el-table__body-wrapper tr {
  height: 80px !important;
}

.Multi_level_header /deep/.el-table__body-wrapper .lineBoxhidden {
  height: 64px !important;
  line-height: 22px !important;
}

.Multi_level_header /deep/.el-table .el-table__body-wrapper .cell {
  height: 100% !important;
}

.materialClass /deep/.el-table tr:nth-child(4) {
  display: none;
}

#materialPage /deep/ .bgwarn2 {
  background: #e6a23c !important;
  color: #fff !important;
}

#materialPage /deep/ .bgwarn1 {
  background: #409eff !important;
  color: #fff !important;
}

#materialPage /deep/ .bgwarn3 {
  background-color: #f56c6c !important;
  color: #fff !important;
}

#materialPage /deep/ .bgwarn4 {
  background-color: rgb(212, 179, 137) !important;
  color: #fff !important;
}

/* 任务单弹框 */
</style>
